﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrImport_Data : DevComponents.DotNetBar.Office2007Form
    {
        public FrImport_Data()
        {
            InitializeComponent();
        }

        private void bt_browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            // Lấy đường dẫn file import vừa chọn
            txt_path.Text = ofd.ShowDialog() == DialogResult.OK ? ofd.FileName : "";
        }

        private void bt_import_Click(object sender, EventArgs e)
        {
            if (txt_path.Text == "")
                MessageBox.Show("Bạn chưa chọn đường dẫn tới file");
            else
            {
                Data dta = new Data();
                SqlConnection Conn = dta.Connect();
                SqlCommand cmd = new SqlCommand();
                try
                {
                    Conn.Open();
                    cmd.Connection = Conn;
                    string brows = null;
                    brows = txt_path.Text;
                    FileInfo f1 = new FileInfo(brows);
                    StreamReader Re = File.OpenText("ImportDataBase.txt");
                    string input = null;
                    int i = 0;
                    while ((input = Re.ReadLine()) != null)
                    {
                        i++;
                        try
                        { 
                            cmd.CommandText = input;
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            string s = ex.Message;
                            string STT=i.ToString();
                            dataGridView1.Rows.Add(STT,s,input);
                        }
                    }
                    MessageBox.Show("Dữ liệu được thêm hoàn tất");
                    Re.Close();
                    cmd.Dispose();
                    Conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    // 
                }
            }
        }
        

        private void bt_delete_Click(object sender, EventArgs e)
        {
            Data dta = new Data();
            DialogResult d = MessageBox.Show("Bạn có chắc muốn XÓA DỮ LIỆU CŨ không? ", "Thông Báo", MessageBoxButtons.OKCancel);
            if (d == DialogResult.OK)
            {
                SqlConnection Conn = dta.Connect();
                SqlCommand cmd = new SqlCommand();
                try
                {
                    Conn.Open();
                    cmd.Connection = Conn;
                    string delete = @"delete from dbo.KH_SLCT where MaSP is not null";
                    cmd.CommandText = delete;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu được xóa hoàn tất");
                    cmd.Dispose();
                    Conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    // 
                }
            }
        }
    }
}
